package com.api.workflow.web;

import com.api.workflow.util.YNHttpUtils;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/*
 *OA-24.资产调拨公司内调拨  走报废
 * 接口8 资产报废接口
 */
public class YNFII013BFAction extends BaseBean implements Action {
    public static void main(String[] args) {

    }

    @Override
    public String execute(RequestInfo requestInfo) {
        String requestid = requestInfo.getRequestid();
        int formid = requestInfo.getRequestManager().getFormid();
        int formidAbs = Math.abs(formid);// 流程表单id绝对值
        String tableName ="formtable_main_"+formidAbs;

        BaseBean baseBean = new BaseBean();
        //主表数据
        MainTableInfo mainTableInfo = requestInfo.getMainTableInfo();
        Property[]  property = mainTableInfo.getProperty();
        //这里是存储了主表全部的数据  可自行修改存储结构
        Map<String, Object> mainTableInfoMap = new HashMap<>();
        for (int i = 0; i < property.length; i++) {
            String name = property[i].getName().toLowerCase();
            mainTableInfoMap.put(name, Util.null2String(property[i].getValue()));
        }
        //明细表数据
        DetailTable[] detailTable=requestInfo.getDetailTableInfo().getDetailTable();
        String maintable = Arrays.toString(property); // 将主表属性数组转换为字符串
        String detailtable = Arrays.deepToString(detailTable); // 将明细表数组转换为字符串
        baseBean.writeLog("lyh===========maintable"+com.alibaba.fastjson.JSONObject.toJSONString(maintable));
        baseBean.writeLog("lyh============detailtable"+com.alibaba.fastjson.JSONObject.toJSONString(detailtable));
        String dblx = String.valueOf(mainTableInfoMap.get("dblx"));
        if (!dblx.equals("ND")) {
            try {
                RecordSet rs = new RecordSet();
                Map<String, Object> map = new HashMap<>();
                //遍历主表属性，提取关键字段
                for (Property property1 : property) {
                    String name = property1.getName().toLowerCase();
                    baseBean.writeLog("lyh============property1" + name + "lyh" + property1.getValue());
                    if (name.equals("lcbh")) {
                        map.put("ZIFSN", property1.getValue());
                    }

                    if (name.equals("sqr")) {
                        String sqr = property1.getValue();
                        String sql1 = "select lastname from hrmresource where id = '" + sqr + "'";
                        rs.execute(sql1);
                        if (rs.next()) {
                            map.put("ZUSER", rs.getString("lastname"));
                            baseBean.writeLog("lyh:OA-24==========lastname" + rs.getString("lastname"));
                        }
                        baseBean.writeLog("lyh:OA-24=============sql1" + sql1);
                    }
                    //处理申请部门信息
                    if (name.equals("sqbm")) {
                        String sqbm = property1.getValue();
                        baseBean.writeLog("lyh:OA-24============sqbm" + sqbm);
                        String sql2 = "select * from hrmdepartment where id = '" + sqbm + "'";
                        rs.execute(sql2);
                        if (rs.next()) {
                            map.put("ZSQBM", rs.getString("departmentcode"));
                            map.put("ZBMMS", rs.getString("departmentname"));
                            baseBean.writeLog("lyh:OA-24=============ZSQBM" + rs.getString("departmentcode") + "ZBMMS:" + rs.getString("departmentname"));
                        }
                    }
                    //处理公司代码
                    if (name.equals("gsdm")) {
                        map.put("BUKRS", property1.getValue());
                    }
                }
                String tableDBName = "";
                ArrayList<Map<String, Object>> list = new ArrayList<>();

                for (DetailTable detailTable1 : detailTable) {
                    tableDBName = detailTable1.getTableDBName();
                    baseBean.writeLog("lyh:OA-24============tableDBName" + tableDBName);
                    int count = 1;
                    for (Row row : detailTable1.getRow()) {
                        Map<String, Object> ITEM = new HashMap<>();
                        for (Cell cell : row.getCell()) {
                            baseBean.writeLog("lyh:OA-24============cell" + cell.getName() + "lyh:OA-24:" + cell.getValue());
                            String name = cell.getName().toLowerCase();
                            //将明细表字段映射到SAP接口字段
                            ITEM.put("ZITEM", count);
                            count++;
                            if (name.equals("xzzc")) {
                                ITEM.put("ANLN1", cell.getValue());
                                RecordSet recordSet = new RecordSet();
                                recordSet.executeUpdate("update uf_zcxxbd set zczt=1 where zckph=?",cell.getValue());
                            }
                            if (name.equals("zcmc")) {
                                ITEM.put("TXT50", cell.getValue());
                            }
                            if (name.equals("dczcyz")) {
                                ITEM.put("ZZCYZ", cell.getValue());
                            }
                            if (name.equals("pzrq")) {
                                ITEM.put("H_BLDAT", cell.getValue());
                            }
                            if (name.equals("gzrq")) {
                                ITEM.put("H_BUDAT", cell.getValue());
                            }
                            if (name.equals("zcqxr")) {
                                ITEM.put("BZDAT", cell.getValue());
                            }
                            if (name.equals("ljzj")) {
                                ITEM.put("ZLJZJ", cell.getValue());
                            }
                            if (name.equals("zcjz")) {
                                ITEM.put("ZZCJZ", cell.getValue());
                            }
                            if (name.equals("ysyyf")) {
                                ITEM.put("ZYYYF", cell.getValue());
                            }
                            if (name.equals("syyf")) {
                                ITEM.put("ZSYYF", cell.getValue());
                            }
                            if (name.equals("gmje")) {
                                ITEM.put("ZBFJE", cell.getValue());
                            }
                            if (name.equals("dbyt")) {
                                ITEM.put("ZBFYY", cell.getValue());
                            }
                            if (name.equals("bfsr")) {
                                ITEM.put("ZBFSR", cell.getValue());
                            }

                        }
                        list.add(ITEM);
                    }
                }
                map.put("ITEMS", list);
                //将Map转换为JSON对象并记录日志
                com.alibaba.fastjson.JSONObject object = new com.alibaba.fastjson.JSONObject(map);
                baseBean.writeLog("lyh:OA-24============object" + object.toJSONString());
                //调用HTTP工具类发送请求
                String post = YNHttpUtils.FII012(object.toJSONString(), YNHttpUtils.token());
                baseBean.writeLog("lyh:OA-24============post" + post);

                JSONObject jsonObject = JSONObject.parseObject(post);

                RecordSet recordSet = new RecordSet();
                //修改主表参数
                String updateMainTableName = "update " + tableName + " set qqcsx = ? , fhjgx = ?  where  requestid = ? ";
                recordSet.executeUpdate(updateMainTableName, object.toString(), post, requestid);
                baseBean.writeLog("wyq==资产报废接口===========updateMainTableName" + updateMainTableName);

                //获取返回的状态和信息
                JSONArray jsonArray = jsonObject.getJSONArray("RETURN");
                for (int i = 0; i < jsonArray.size(); i++) {
                    JSONObject jsonObject1 = jsonArray.getJSONObject(0);
                    String TYPE = jsonObject1.getString("TYPE");
                    String MESSAGE = jsonObject1.getString("MESSAGE");

                    if (!"S".equals(TYPE)) {
                        requestInfo.getRequestManager()
                                .setMessageid(requestid + "OA-资产报废接口");
                        requestInfo.getRequestManager().setMessagecontent("SAP接口异常，请联系SAP人员，错误信息提示:" + MESSAGE);
                        return Action.FAILURE_AND_CONTINUE;
                    }

                }

                baseBean.writeLog("lyh:OA-24==============资产报废接口");
                //如果状态返回值不成功，推送错误信息


            } catch (Exception e) {
                //捕获异常并记录日志
                baseBean.writeLog("lyh:OA-24=========exc" + e.toString());
                requestInfo.getRequestManager()
                        .setMessageid(requestid + "OA-资产报废接口");
                requestInfo.getRequestManager().setMessagecontent("OA接口异常：" + e.toString());
                return Action.FAILURE_AND_CONTINUE;
            }
        }
        return Action.SUCCESS;
    }

}
